package com.yiwyn.Structural.proxy;


/**
 * 代理模式demo
 */
public class ProxyDemo {


    public static void main(String[] args) {
        IUserService userService = new UserProxy();
        userService.saveUser();
    }


    interface IUserService {
        void saveUser();
    }

    static class UserService implements IUserService {
        @Override
        public void saveUser() {
            System.out.println("保存用户信息！");
        }
    }

    static class UserProxy implements IUserService {

        private IUserService userService;

        public UserProxy() {
            this.userService = new UserService();
        }

        @Override
        public void saveUser() {
            System.out.println(UserProxy.class.getSimpleName() + "开启事务");

            try {
                userService.saveUser();
            } catch (Exception e) {
                System.out.println("事务回滚");
                throw new RuntimeException(e);
            }

            System.out.println(UserProxy.class.getSimpleName() + "事务提交");
        }
    }


}
